package com.example.wanbaqian.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.activity.AboutUsActivity;
import com.example.wanbaqian.activity.ApplyOneActivity;
import com.example.wanbaqian.activity.FeedbackActivity;
import com.example.wanbaqian.activity.FindBankActivity;
import com.example.wanbaqian.activity.FindMessageActivity;
import com.example.wanbaqian.activity.HelpCenterActivity;
import com.example.wanbaqian.activity.UserInfoActivity;
import com.example.wanbaqian.base.BaseFragment;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.ApplyOneBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.UserInfoBean;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 个人中心--fragment.
 */
public class MyFragment extends BaseFragment implements HttpCallback {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.my_userIcon)
    ImageView myUserIcon;
    @BindView(R.id.my_userName)
    TextView my_userName;
    @BindView(R.id.txt_phone)
    TextView mPhone;
    @BindView(R.id.my_bTv)
    TextView myBTv;
    @BindView(R.id.my_helpOnline)
    TextView myHelpOnline;
    @BindView(R.id.my_helpMobile)
    TextView myHelpMobile;
    private UserInfoBean userInfo;

    public static MyFragment newInstance(String param1) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initViewsAndEvents(View view) {

    }

    @Override
    public void onResume() {
        super.onResume();
        userInfo = SPUtils.getUserInfo(mContext);

        //2017年9月20日10:09:49修改

//        String s = (String) SPUtils.get(mContext, CommonValue.userInfo, "");
//        Log.e("LOG", "onResume: "+s);

//        String replace = s.replace("\\", "");

//        Log.e("LOG", "onResume: "+replace);

//        Gson gson = new GsonBuilder()
//                .setPrettyPrinting()
//                .disableHtmlEscaping()
//                .create();
//        Gson gson = new Gson();
//        Type type = new TypeToken<UserInfoBean>(){}.getType();
//        userInfo = gson.fromJson(replace, type);

//        Log.e("LOG", "userinfo 解析失败？？onResume: "+userInfo.toString());

        BaseUtils.loadImageForUserIcon(mContext, ""+userInfo.getAvatar_url(), myUserIcon);
        mPhone.setText(userInfo.getLogin_mobile());
        my_userName.setText(userInfo.getNick_name());

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void DetoryViewAndThing() {

    }

    @OnClick({R.id.my_userIcon, R.id.my_item_xLayout, R.id.my_item_bLayout, R.id.my_item_meLayout,
            R.id.my_item_optionLayout, R.id.my_item_aboutLayout, R.id.my_item_helpLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            //我的资料
            case R.id.my_item_meLayout:
                HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_MATERIAL, mContext,
                        new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)}), mContext, null, CommonValue.REQUEST_CODE1, this);
                break;
            //银行卡
            case R.id.my_item_bLayout:
                openActivity(FindBankActivity.class);
                break;

            //意见反馈
            case R.id.my_item_optionLayout:
                openActivity(FeedbackActivity.class);
                break;
            //我的消息
            case R.id.my_item_xLayout:
                openActivity(FindMessageActivity.class);
                break;
            //帮助中心
            case R.id.my_item_helpLayout:
                openActivity(HelpCenterActivity.class);
                break;
            //关于我们
            case R.id.my_item_aboutLayout:
                openActivity(AboutUsActivity.class);
                break;
            case R.id.my_userIcon:
                openActivity(UserInfoActivity.class);
                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putSerializable("bean", JsonUtils.getInstance().jsonToObjectk(content, ApplyOneBean.class));
                openActivity(ApplyOneActivity.class, bundle);
                break;
            case CommonValue.REQUEST_CODE2:
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
